package org.apache.kafka.niezhipeng.simpleDemo;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.time.Duration;
import java.util.Collections;
import java.util.Properties;


/**
 * @author Nie ZhiPeng
 * @date 2025/7/17 23:20
 */
public class SimpleConsumer {

    public static void main(String[] args) {
        Properties properties = SimpleDemoUtil.getConsumerProperties();

        KafkaConsumer<String, String> kafkaConsumer = new KafkaConsumer<>(properties);
        kafkaConsumer.subscribe(Collections.singleton(SimpleDemoUtil.TOPIC));
        while (true) {
            ConsumerRecords<String, String> records = kafkaConsumer.poll(Duration.ofMillis(1000));
            for (ConsumerRecord<String, String> record : records) {
                String value = record.value();
                System.out.printf("message:[%s], partition:[%d]\n", record.value(), record.partition());
                if ("stop".equals(value)) {
                    return;
                }
            }
        }
    }
}
